package com.acsior.hibernate.vo;

// default package

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.acsior.dac.impl.hibernate.vo.GeneratorKey;
import com.acsior.dac.impl.hibernate.vo.HibernateVO;

/**
 * CdCashDesk entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "cdCashDesks")
@GeneratorKey(tableName = "cdCashDesks")
public class CdCashDeskVO extends HibernateVO {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -3925243438730526453L;

	@Id
	@Column(name = "cashDeskID", unique = true, nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long cashDeskId;

	@Column(name = "cashRegisterID", nullable = false)
	private String cashRegisterId;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "cdCashDesk")
	private List<CdReceiptHeaderVO> cdReceiptHeaders;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "cdCashDesk")
	private List<CdReceiptsCanceledVO> cdReceiptsCanceleds;

	@Column(name = "closeTime", length = 23)
	private Date closeTime;

	@Column(name = "crOperatorID", nullable = false)
	private Integer crOperatorId;

	@Column(name = "isActive", nullable = false)
	private boolean isActive;

	@Column(name = "lastAccessTime", nullable = false, length = 23)
	private Date lastAccessTime;

	@Column(name = "lastActiveIP", nullable = false)
	private String lastActiveIp;

	@Column(name = "objectID", nullable = false)
	private String objectId;

	@Column(name = "openTime", nullable = false, length = 23)
	private Date openTime;

	@Column(name = "operatorID", nullable = false)
	private String operatorId;

	// Constructors

	/** default constructor */
	public CdCashDeskVO() {
		this.cdReceiptHeaders = new ArrayList<CdReceiptHeaderVO>();
		this.cdReceiptsCanceleds = new ArrayList<CdReceiptsCanceledVO>();
	}

	/**
	 * @return стойността на cashDeskId
	 */
	public long getCashDeskId() {
		return this.cashDeskId;
	}

	/**
	 * @param cashDeskId
	 *            се залага в cashDeskId
	 */
	public void setCashDeskId(long cashDeskId) {
		this.cashDeskId = cashDeskId;
	}

	/**
	 * @return стойността на cashRegisterId
	 */
	public String getCashRegisterId() {
		return this.cashRegisterId;
	}

	/**
	 * @param cashRegisterId
	 *            се залага в cashRegisterId
	 */
	public void setCashRegisterId(String cashRegisterId) {
		this.cashRegisterId = cashRegisterId;
	}

	/**
	 * @return стойността на cdReceiptHeaders
	 */
	public List<CdReceiptHeaderVO> getCdReceiptHeaders() {
		return this.cdReceiptHeaders;
	}

	/**
	 * @param cdReceiptHeaders
	 *            се залага в cdReceiptHeaders
	 */
	public void setCdReceiptHeaders(List<CdReceiptHeaderVO> cdReceiptHeaders) {
		this.cdReceiptHeaders = cdReceiptHeaders;
	}

	/**
	 * @return стойността на cdReceiptsCanceleds
	 */
	public List<CdReceiptsCanceledVO> getCdReceiptsCanceleds() {
		return this.cdReceiptsCanceleds;
	}

	/**
	 * @param cdReceiptsCanceleds
	 *            се залага в cdReceiptsCanceleds
	 */
	public void setCdReceiptsCanceleds(
			List<CdReceiptsCanceledVO> cdReceiptsCanceleds) {
		this.cdReceiptsCanceleds = cdReceiptsCanceleds;
	}

	/**
	 * @return стойността на closeTime
	 */
	public Date getCloseTime() {
		return this.closeTime;
	}

	/**
	 * @param closeTime
	 *            се залага в closeTime
	 */
	public void setCloseTime(Date closeTime) {
		this.closeTime = closeTime;
	}

	/**
	 * @return стойността на crOperatorId
	 */
	public Integer getCrOperatorId() {
		return this.crOperatorId;
	}

	/**
	 * @param crOperatorId
	 *            се залага в crOperatorId
	 */
	public void setCrOperatorId(Integer crOperatorId) {
		this.crOperatorId = crOperatorId;
	}

	/**
	 * @return стойността на isActive
	 */
	public boolean isActive() {
		return this.isActive;
	}

	/**
	 * @param isActive
	 *            се залага в isActive
	 */
	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	/**
	 * @return стойността на lastAccessTime
	 */
	public Date getLastAccessTime() {
		return this.lastAccessTime;
	}

	/**
	 * @param lastAccessTime
	 *            се залага в lastAccessTime
	 */
	public void setLastAccessTime(Date lastAccessTime) {
		this.lastAccessTime = lastAccessTime;
	}

	/**
	 * @return стойността на lastActiveIp
	 */
	public String getLastActiveIp() {
		return this.lastActiveIp;
	}

	/**
	 * @param lastActiveIp
	 *            се залага в lastActiveIp
	 */
	public void setLastActiveIp(String lastActiveIp) {
		this.lastActiveIp = lastActiveIp;
	}

	/**
	 * @return стойността на objectId
	 */
	public String getObjectId() {
		return this.objectId;
	}

	/**
	 * @param objectId
	 *            се залага в objectId
	 */
	public void setObjectId(String objectId) {
		this.objectId = objectId;
	}

	/**
	 * @return стойността на openTime
	 */
	public Date getOpenTime() {
		return this.openTime;
	}

	/**
	 * @param openTime
	 *            се залага в openTime
	 */
	public void setOpenTime(Date openTime) {
		this.openTime = openTime;
	}

	/**
	 * @return стойността на operatorId
	 */
	public String getOperatorId() {
		return this.operatorId;
	}

	/**
	 * @param operatorId
	 *            се залага в operatorId
	 */
	public void setOperatorId(String operatorId) {
		this.operatorId = operatorId;
	}

	/**
	 * @see com.acsior.dac.data.Data#getId()
	 */
	@Override
	public Object getId() {
		return getCashDeskId();
	}

	/**
	 * @see com.acsior.dac.impl.hibernate.vo.HibernateVO#setId(java.lang.Object)
	 */
	@Override
	public void setId(Object id) {
		// този така или иначе не го викат
		setCashDeskId(Long.class.cast(id));
	}

	/**
	 * Понеже търсенето е в DB слоя го слагам тук. Това се използва в БО, а
	 * бележката сама по себе си все още няма такъв, защото не е необходим.
	 * Използването на метода е свързан с бизнеса на главна каса. Стори ми се
	 * минимално решение.
	 * 
	 * @param UIN
	 *            номера на UIN-a
	 * @return намерената инстанция или <code>null</code> ако няма такава
	 */
	public CdReceiptHeaderVO locateMagicalLastNote(int UIN) {
		List<CdReceiptHeaderVO> list = getCdReceiptHeaders();

		// най-новите са първи
		Collections.sort(list, new Comparator<CdReceiptHeaderVO>() {
			@Override
			public int compare(CdReceiptHeaderVO o1, CdReceiptHeaderVO o2) {
				long a = o1.getRegistrationTime().getTime();
				long b = o2.getRegistrationTime().getTime();
				return a < b ? 1 : a > b ? -1 : 0;
			}
		});

		for (CdReceiptHeaderVO r : list) {
			if (r.getReceiptUin() == UIN) {
				return r;
			}
		}
		
		return null;
	}
}